<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Mapping character filter | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'analysis-mapping-charfilter.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/analysis-mapping-charfilter.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/analysis-mapping-charfilter.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/analysis-mapping-charfilter.html" rel="nofollow" target="_blank">../en/analysis-mapping-charfilter.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="analysis.html">Text analysis</a></span>
»
<span class="breadcrumb-link"><a href="analysis-charfilters.html">Character filters reference</a></span>
»
<span class="breadcrumb-node">Mapping character filter</span>
</div>
<div class="navheader">
<span class="prev">
<a href="analysis-htmlstrip-charfilter.html">« HTML strip character filter</a>
</span>
<span class="next">
<a href="analysis-pattern-replace-charfilter.html">Pattern Replace Char Filter »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="analysis-mapping-charfilter"></a>Mapping character filter<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/analysis/charfilters/mapping-charfilter.asciidoc">edit</a>
</h2>
</div></div></div>

<p>The <code class="literal">mapping</code> character filter accepts a map of keys and values.  Whenever it
encounters a string of characters that is the same as a key, it replaces them
with the value associated with that key.</p>
<p>Matching is greedy; the longest pattern matching at a given point wins.
Replacements are allowed to be the empty string.</p>
<p>The <code class="literal">mapping</code> filter uses Lucene’s
<a href="https://lucene.apache.org/core/8_5_1/analyzers-common/org/apache/lucene/analysis/charfilter/MappingCharFilter.html" class="ulink" target="_top">MappingCharFilter</a>.</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="analysis-mapping-charfilter-analyze-ex"></a>Example<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/analysis/charfilters/mapping-charfilter.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The following <a class="xref" href="indices-analyze.html" title="Analyze API">analyze API</a> request uses the <code class="literal">mapping</code> filter
to convert Hindu-Arabic numerals (٠‎١٢٣٤٥٦٧٨‎٩‎) into their Arabic-Latin
equivalents (0123456789), changing the text <code class="literal">My license plate is ٢٥٠١٥</code> to
<code class="literal">My license plate is 25015</code>.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /_analyze
{
  "tokenizer": "keyword",
  "char_filter": [
    {
      "type": "mapping",
      "mappings": [
        "٠ =&gt; 0",
        "١ =&gt; 1",
        "٢ =&gt; 2",
        "٣ =&gt; 3",
        "٤ =&gt; 4",
        "٥ =&gt; 5",
        "٦ =&gt; 6",
        "٧ =&gt; 7",
        "٨ =&gt; 8",
        "٩ =&gt; 9"
      ]
    }
  ],
  "text": "My license plate is ٢٥٠١٥"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1022.console"></div>
<p>The filter produces the following text:</p>
<div class="pre_wrapper lang-text">
<pre class="programlisting prettyprint lang-text">[ My license plate is 25015 ]</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="analysis-mapping-charfilter-configure-parms"></a>Configurable parameters<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/analysis/charfilters/mapping-charfilter.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">mappings</code>
</span>
</dt>
<dd>
<p>
(Required*, array of strings)
Array of mappings, with each element having the form <code class="literal">key =&gt; value</code>.
</p>
<p>Either this or the <code class="literal">mappings_path</code> parameter must be specified.</p>
</dd>
<dt>
<span class="term">
<code class="literal">mappings_path</code>
</span>
</dt>
<dd>
<p>
(Required*, string)
Path to a file containing <code class="literal">key =&gt; value</code> mappings.
</p>
<p>This path must be absolute or relative to the <code class="literal">config</code> location, and the file
must be UTF-8 encoded. Each mapping in the file must be separated by a line
break.</p>
<p>Either this or the <code class="literal">mappings</code> parameter must be specified.</p>
</dd>
</dl>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="analysis-mapping-charfilter-customize"></a>Customize and add to an analyzer<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/analysis/charfilters/mapping-charfilter.asciidoc">edit</a>
</h3>
</div></div></div>
<p>To customize the <code class="literal">mappings</code> filter, duplicate it to create the basis for a new
custom character filter. You can modify the filter using its configurable
parameters.</p>
<p>The following <a class="xref" href="indices-create-index.html" title="Create index API">create index API</a> request
configures a new <a class="xref" href="analysis-custom-analyzer.html" title="Create a custom analyzer">custom analyzer</a> using a custom
<code class="literal">mappings</code> filter, <code class="literal">my_mappings_char_filter</code>.</p>
<p>The <code class="literal">my_mappings_char_filter</code> filter replaces the <code class="literal">:)</code> and <code class="literal">:(</code> emoticons
with a text equivalent.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "char_filter": [
            "my_mappings_char_filter"
          ]
        }
      },
      "char_filter": {
        "my_mappings_char_filter": {
          "type": "mapping",
          "mappings": [
            ":) =&gt; _happy_",
            ":( =&gt; _sad_"
          ]
        }
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1023.console"></div>
<p>The following <a class="xref" href="indices-analyze.html" title="Analyze API">analyze API</a> request uses the custom
<code class="literal">my_mappings_char_filter</code> to replace <code class="literal">:(</code> with <code class="literal">_sad_</code> in
the text <code class="literal">I'm delighted about it :(</code>.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /my_index/_analyze
{
  "tokenizer": "keyword",
  "char_filter": [ "my_mappings_char_filter" ],
  "text": "I'm delighted about it :("
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1024.console"></div>
<p>The filter produces the following text:</p>
<div class="pre_wrapper lang-text">
<pre class="programlisting prettyprint lang-text">[ I'm delighted about it _sad_ ]</pre>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="analysis-htmlstrip-charfilter.html">« HTML strip character filter</a>
</span>
<span class="next">
<a href="analysis-pattern-replace-charfilter.html">Pattern Replace Char Filter »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>